﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CRMFramework;
using System.Collections;
using Newtonsoft.Json;
using CRMEntity;
using Microsoft.VisualBasic;
using CRMBiz.AM;

namespace CRMBiz
{
    public class CalPayment
    {

        public static DataTable CalculatePaymentbyFre(int LoanID, string paraPurpose, string paraProduct, string paraType, string paraSettlementDate,
                                                      int paraTerm, string paraFirstPaymentDate, string paraPaymetFrequecy, decimal paraAnnumInterestRate,
                                                      string paraInterestMethod, decimal paraLoanAmount)
        {

            DateTime FirstPaymentDate = DateTime.Parse(paraFirstPaymentDate);
            DateTime SettlementDate = DateTime.Parse(paraSettlementDate);
            int daysFirstPeriod;
            decimal dailyInterestRate, dailyInterestAmount;
            DataTable dtSchedule = new DataTable();

            //Create DataTable
            DataTable table = new DataTable();
            table.Columns.Add("amount", typeof(decimal));
            table.Columns.Add("PaymetFrequecy", typeof(string));
            //End Create DataTable
            try
            {
                #region Simple
                if (paraInterestMethod == "Simple")
                {
                    if (paraProduct == "Unsecured Business Loan")
                    {
                        if (paraType == "P And I")
                        {
                            if (paraPaymetFrequecy == "Weekly")
                            {
                                daysFirstPeriod = (FirstPaymentDate - SettlementDate).Days;
                                dailyInterestRate = paraAnnumInterestRate / 365;
                                dailyInterestAmount = paraLoanAmount * dailyInterestRate;
                                dailyInterestAmount = dailyInterestAmount * 7;
                                table.Rows.Add((Math.Round((dailyInterestAmount), 2)), "Weekly");
                            }
                            else if (paraPaymetFrequecy == "Fortnightly")
                            {
                                daysFirstPeriod = (FirstPaymentDate - SettlementDate).Days;
                                dailyInterestRate = paraAnnumInterestRate / 365;
                                dailyInterestAmount = paraLoanAmount * dailyInterestRate;
                                dailyInterestAmount = dailyInterestAmount * 14;
                                table.Rows.Add((Math.Round((dailyInterestAmount), 2)), "Fortnightly");
                            }
                            else if (paraPaymetFrequecy == "Monthly")
                            {
                                daysFirstPeriod = (FirstPaymentDate - SettlementDate).Days;
                                dailyInterestRate = paraAnnumInterestRate / 365;
                                dailyInterestAmount = paraLoanAmount * dailyInterestRate;
                                dailyInterestAmount = dailyInterestAmount * 30;
                                table.Rows.Add((Math.Round((dailyInterestAmount), 2)), "Monthly");
                            }
                            else if (paraPaymetFrequecy == "Daily(Mon-Fri)")
                            {
                                daysFirstPeriod = (FirstPaymentDate - SettlementDate).Days;
                                dailyInterestRate = paraAnnumInterestRate / 365;
                                dailyInterestAmount = paraLoanAmount * dailyInterestRate;
                                table.Rows.Add((Math.Round((dailyInterestAmount), 2)), "Daily");
                            }
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return table;
        }

        public static List<PaymentEntity> SimpleInterestPaymentScheduleByTerm(int LoanID, string paraPurpose, string paraProduct, string paraType, string paraSettlementDate,
                                                               int paraTerm, string paraFirstPaymentDate, string paraPaymetFrequecy, decimal paraAnnumInterestRate,
                                                               string paraInterestMethod, decimal paraLoanAmount)
        {
            /* for payment schedule */
            List<PaymentEntity> paymentScheduleList;
            paymentScheduleList = new List<PaymentEntity>();

            //Create DataTable
            DateTime FirstPaymentDate = DateTime.Parse(paraFirstPaymentDate);
            DateTime SettlementDate = DateTime.Parse(paraSettlementDate);
            int daysFirstPeriod, daysBetweenRepayments = 0;
            decimal dailyInterestRate, dailyInterestAmount;
            DataTable dtSchedule = new DataTable();
            dtSchedule.Columns.Add("DueDate", typeof(DateTime));
            dtSchedule.Columns.Add("Repayment", typeof(decimal));
            dtSchedule.Columns.Add("Principal", typeof(decimal));
            dtSchedule.Columns.Add("Interest", typeof(decimal));
            dtSchedule.Columns.Add("Balance", typeof(decimal));
            //End Create DataTable

            try
            {
                if (paraLoanAmount > 0)
                {
                    #region Simple
                    if (paraInterestMethod == "Simple")
                    {
                        if (paraProduct == "Unsecured Business Loan")
                        {
                            if (paraType == "P And I")
                            {
                                if (paraPaymetFrequecy == "Weekly")
                                {
                                    daysBetweenRepayments = 7;
                                    daysFirstPeriod = (FirstPaymentDate - SettlementDate).Days;
                                    dailyInterestRate = paraAnnumInterestRate / 365;
                                    dailyInterestAmount = paraLoanAmount * dailyInterestRate;
                                    dailyInterestAmount = dailyInterestAmount * 7;

                                    for (int i = 0; paraLoanAmount > 0; i++)
                                    {
                                        DataRow dr = dtSchedule.NewRow();
                                        dr["DueDate"] = SettlementDate.AddDays(i * daysBetweenRepayments);
                                        if (dailyInterestAmount < paraLoanAmount)
                                            dr["Repayment"] = dailyInterestAmount;
                                        else
                                            dr["Repayment"] = paraLoanAmount;
                                        dr["Balance"] = paraLoanAmount;
                                        paraLoanAmount = paraLoanAmount - dailyInterestAmount;
                                        dtSchedule.Rows.Add(dr);
                                    }
                                }
                                if (paraPaymetFrequecy == "Fortnightly")
                                {
                                    daysBetweenRepayments = 14;
                                    daysFirstPeriod = (FirstPaymentDate - SettlementDate).Days;
                                    dailyInterestRate = paraAnnumInterestRate / 365;
                                    dailyInterestAmount = paraLoanAmount * dailyInterestRate;
                                    dailyInterestAmount = dailyInterestAmount * 14;

                                    for (int i = 0; i < paraLoanAmount; i++)
                                    {
                                        DataRow dr = dtSchedule.NewRow();
                                        dr["DueDate"] = SettlementDate.AddDays(i * daysBetweenRepayments);
                                        if (dailyInterestAmount < paraLoanAmount)
                                            dr["Repayment"] = dailyInterestAmount;
                                        else
                                            dr["Repayment"] = paraLoanAmount;
                                        dr["Balance"] = paraLoanAmount;
                                        paraLoanAmount = paraLoanAmount - dailyInterestAmount;
                                        dtSchedule.Rows.Add(dr);
                                    }
                                }
                                if (paraPaymetFrequecy == "Monthly")
                                {
                                    daysBetweenRepayments = 30;
                                    daysFirstPeriod = (FirstPaymentDate - SettlementDate).Days;
                                    dailyInterestRate = paraAnnumInterestRate / 365;
                                    dailyInterestAmount = paraLoanAmount * dailyInterestRate;
                                    dailyInterestAmount = dailyInterestAmount * 30;

                                    for (int i = 0; i < paraLoanAmount; i++)
                                    {
                                        DataRow dr = dtSchedule.NewRow();
                                        dr["DueDate"] = SettlementDate.AddDays(i * daysBetweenRepayments);
                                        if (dailyInterestAmount < paraLoanAmount)
                                            dr["Repayment"] = dailyInterestAmount;
                                        else
                                            dr["Repayment"] = paraLoanAmount;
                                        dr["Balance"] = paraLoanAmount;
                                        paraLoanAmount = paraLoanAmount - dailyInterestAmount;
                                        dtSchedule.Rows.Add(dr);
                                    }
                                }
                                if (paraPaymetFrequecy == "Daily(Mon-Fri)")
                                {
                                    daysBetweenRepayments = 1;
                                    daysFirstPeriod = (FirstPaymentDate - SettlementDate).Days;
                                    dailyInterestRate = paraAnnumInterestRate / 365;
                                    dailyInterestAmount = paraLoanAmount * dailyInterestRate;

                                    for (int i = 0; i < paraLoanAmount; i++)
                                    {
                                        DataRow dr = dtSchedule.NewRow();

                                        if (SettlementDate.DayOfWeek.ToString() != "Saturday" && SettlementDate.DayOfWeek.ToString() != "Sunday")
                                        {
                                            dr["DueDate"] = SettlementDate;
                                            if (dailyInterestAmount < paraLoanAmount)
                                                dr["Repayment"] = dailyInterestAmount;
                                            else
                                                dr["Repayment"] = paraLoanAmount;
                                            dr["Balance"] = paraLoanAmount;
                                            paraLoanAmount = paraLoanAmount - dailyInterestAmount;
                                            dtSchedule.Rows.Add(dr);
                                        }
                                        else
                                        {
                                            i = i - 1;
                                        }
                                        SettlementDate = SettlementDate.AddDays(1);
                                    }
                                }
                            }
                        }
                    }
                    #endregion
                }
                PaymentEntity objPaymentEntity = null;
                foreach (DataRow dr in dtSchedule.Rows)
                {
                    objPaymentEntity = new PaymentEntity();
                    objPaymentEntity.date = Convert.ToDateTime(dr["DueDate"].ToString());
                    objPaymentEntity.day = objPaymentEntity.date.DayOfWeek.ToString();
                    objPaymentEntity.amount = Convert.ToDecimal(dr["Repayment"].ToString());
                    objPaymentEntity.Balance = Convert.ToDecimal(dr["Balance"].ToString());
                    paymentScheduleList.Add(objPaymentEntity);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return paymentScheduleList;
        }

    }
}
